<?php

namespace App\Jobs\Order;

use App\Facade\Elastic\ElasticsearchFacade;
use App\Models\Order\Order;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldBeUnique;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;

class OrderSyncToEsJob implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    // 通过重写 connection() 方法指定队列连接
    public function connection()
    {
        return 'rabbitmq'; // 这里返回你需要的队列连接名称
    }

    private $startId;
    private $endId;
    private $indexName = 'order_full_0813';

    public function __construct(int $startId, int $endId)
    {
        $this->startId = $startId;
        $this->endId = $endId;
    }
    public function handle()
    {
        echo "startId:{$this->startId},endId:{$this->endId}\n";
        Order::whereBetween('id',[$this->startId, $this->endId])->chunk(100, function ($items) {
            foreach ($items as $item) {
                ElasticsearchFacade::indexDocument($this->indexName, $item->id, $item->toArray());
            }
        });
    }
}
